<?php
namespace app\admin\model;

use think\Model;
use think\Db;
use think\facade\Config;
class Events extends Model
{   
   protected $connection = 'publicdata';
    public function eventsclass()
    {
        return $this->hasOne('Eventsclass','class_id','class_id');
    }
	/*查询数据 */
	public function get_data($page,$limit){


        $start=($page-1)*$limit;
        $list=Events::with('eventsclass')->where([['status','=',1]])->order('id', 'desc')->limit($start,$limit)->select()->toArray();
       
        $listarr=array();
        foreach ($list as $key => $value) {
            foreach ($value as $k => $v) {
                
                if($k!='eventsclass'){
                   $listarr[$key][$k]=$v;
                }else{
                   $listarr[$key][$k]=$v['title'];
                }
            }
        }
        
        $data['count']=Events::count();
        $data['list']=$listarr;

        return $data;
        
	}
	/*获取一条数据用于修改*/
	public function get_edit($id){
        $data=Events::with('eventsclass')->where([['status','=',1],['id','=',$id]])->find();
        return $data;  
	}
    
	/*新增*/
	public function insert_data($title,$author,$newname,$content,$seo_title,$seo_key,$seo_desc,$url,$class_zhi,$miaodian){
        $Events = new Events;
		$result=$Events->save([
		    'title'  =>  trim($title),
		    'author' =>  trim($author),
            'content' =>  trim($content),
            'seo_title' =>  trim($seo_title),
            'seo_key' =>  trim($seo_key),
            'seo_desc' =>  trim($seo_desc),
		    'url' =>  trim($url),
            'img' =>  trim($newname),
		    'class_id' =>  trim($class_zhi),
		    'create_time' => date("Y-m-d H:i:s",time()),
            'miaodian'  =>  $miaodian
		]);
		return $result;
	}
	/*修改*/
	public function update_data($id,$title,$author,$newname,$content,$seo_title,$seo_key,$seo_desc,$url,$class_zhi,$miaodian){
        $data = Events::get($id);
        $data->title = $title;
        $data->img = $newname;
        $data->author = $author;
        $data->content = $content;
        $data->seo_title = $seo_title;
        $data->seo_key = $seo_key;
        $data->seo_desc = $seo_desc;
        $data->url = $url;
        $data->class_id = $class_zhi;
        $data->miaodian = $miaodian;
        $result=$data->save();
		return $result;    
	}
	/*删除假动作实际为修改操作*/
	public function delete_data($id){
        $data = Events::get($id);
        if($data['status']!=1){
        	$result=['status'=>'0','msg'=>'无需操作'];
	        return json($result);
        }
		$data->status = '2';
		$result=$data->save();
		return $result;
	}
}
 